Code
library(rnaturalearth)
library(mapview)
france <- ne_countries(country = "France", scale = 50)
(france_map <- mapview(france, label = "geounit", legend = FALSE))PPOL 6805 / DSAN 6750: GIS for Spatial Data Science
Fall 2024
sf Cheatsheetsf and terra.shp et al.)A shape“file” is actually (at least) three separate files bundled together:
.shp: Containing feature geometries.shx: Positional indices.dbf: Data attributes.prj: Coordinate reference system.xml: MetadataLet’s see what’s inside the shapefile we first saw in Week 1, containing data on DC’s Census Tracts: Census Tracts in 2020


.geojson).csvmy_data.geojson
{
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"geometry": {
"type": "Polygon",
"coordinates": [
[
[30, 20], [45, 40],
[10, 40], [30, 20]
]
]
},
"properties": {
"color": "green",
"area": 3565747
}
},
{
"type": "Feature",
"geometry": {
"type": "Polygon",
"coordinates": [
[
[15, 5], [40, 10],
[10, 20], [5, 10],
[15, 5]
]
]
},
"properties": {
"color": "red",
"area": 3272386
}
}
]
}.gpkg).tif).nc4)EPSG (European Petroleum Survey Group) Registry: Most common way to specify a CRS
PROJ: Rather than opaque numeric code like EPSG, uses plaintext “proj-strings” containing parameter info: datum, ellipsoid, projection, and units (e.g. meters). Example: PROJ4 code EPSG:4326 is represented as
+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defsHere we’ll look at Africa:
Using rnaturalearth with mapview
library(rnaturalearth)
library(mapview)
france <- ne_countries(country = "France", scale = 50)
(france_map <- mapview(france, label = "geounit", legend = FALSE))library(sf)Linking to GEOS 3.11.0, GDAL 3.5.3, PROJ 9.1.0; sf_use_s2() is TRUE
france_cent <- sf::st_centroid(france)Warning: st_centroid assumes attributes are constant over geometries
france_map + mapview(france_cent, label = "Centroid", legend = FALSE)Computing the union of all geometries in the sf via sf::st_union()
library(leaflet.extras2)Loading required package: leaflet
africa <- ne_countries(continent = "Africa", scale = 50)
africa_union <- sf::st_union(africa)
africa_map <- mapview(africa, label="geounit", legend=FALSE)
africa_union_map <- mapview(africa_union, label="st_union(africa)", legend=FALSE)
africa_map | africa_union_mapafrica_bbox <- sf::st_bbox(africa)
africa_bbox_map <- mapview(africa_bbox, label="st_bbox(africa)", legend=FALSE)
africa_map | africa_bbox_mapafrica_countries_cvx <- sf::st_convex_hull(africa)
africa_countries_cvx_map <- mapview(africa_countries_cvx, label="geounit", legend=FALSE)
africa_map | africa_countries_cvx_mapUse st_union() first:
africa_cvx <- africa |> st_union() |> st_convex_hull()
africa_cvx_map <- mapview(africa_cvx, label="geounit", legend=FALSE)
africa_map | africa_cvx_mapComputing the centroid of all geometries in the sf via sf::st_centroid()
africa_cents <- sf::st_centroid(africa)Warning: st_centroid assumes attributes are constant over geometries
africa_cents_map <- mapview(africa_cents, label="geounit", legend=FALSE)
africa_map | africa_cents_mapsa <- ne_countries(country = "South Africa", scale = 50)
lesotho <- ne_countries(country = "Lesotho", scale=50)
sf::st_relate(lesotho, sa)although coordinates are longitude/latitude, st_relate assumes that they are
planar
[,1]
[1,] "FF2F1F212"
sf::st_covers(lesotho, sa)Sparse geometry binary predicate list of length 1, where the predicate
was `covers'
1: (empty)
sf::st_covered_by(lesotho, sa)Sparse geometry binary predicate list of length 1, where the predicate
was `covered_by'
1: (empty)
mapview(sa) + mapview(lesotho)